package com.hopeland.androidpc.jilintongdaomen;

import android.app.Application;
import android.content.Context;
import android.os.Message;
import android.text.TextUtils;


import com.hopeland.androidpc.jilintongdaomen.network.MainApi;
import com.tencent.bugly.crashreport.CrashReport;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


import okhttp3.OkHttpClient;

/**
 * Created by Administrator on 2018/8/28/028.
 */

public class App extends Application {

    public static App application;
    @Override
    public void onCreate() {
        super.onCreate();
       // Thread.setDefaultUncaughtExceptionHandler(restartHandler);

        application = this;
        initBugly();
        MainApi.getInstance().setApplication(App.this);

    }


//    private void initBugly() {
//        Context context = getApplicationContext();
//        // 获取当前包名
//        String packageName = context.getPackageName();
//        // 获取当前进程名
//        String processName = getProcessName(android.os.Process.myPid());
//        // 设置是否为上报进程
//        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context);
//        strategy.setUploadProcess(processName == null || processName.equals(packageName));
//        // 初始化Bugly
//        Bugly.init(getApplicationContext(), "729695364e", false, strategy);
//    }

    private void initBugly() {
        Context context = getApplicationContext();
        // 获取当前包名
        String packageName = context.getPackageName();
        // 获取当前进程名
        String processName = getProcessName(android.os.Process.myPid());
        // 设置是否为上报进程
        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context);
//        strategy.setDeviceID(AppUtil.getSerialEncryptionNum());
//        // 设置anr时是否获取系统trace文件，默认为false
//        strategy.setEnableCatchAnrTrace(true);
//        // 设置是否获取anr过程中的主线程堆栈，默认为true
//        strategy.setEnableRecordAnrMainStack(true);
        strategy.setUploadProcess(processName == null || processName.equals(packageName));
        // 初始化Bugly b isDebug,true为正式版启用，false为测试版启用
        CrashReport.initCrashReport(context, "729695364e", true, strategy);
    }

    private static String getProcessName(int pid) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
            String processName = reader.readLine();
            if (!TextUtils.isEmpty(processName)) {
                processName = processName.trim();
            }
            return processName;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
        return null;
    }



    public void restartApp() {
//        Intent intent = new Intent(this, MainActivity.class);
//        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
//        startActivity(intent);
//        android.os.Process.killProcess(android.os.Process.myPid());  //结束进程之前可以把你程序的注销或者退出代码放在这段代码之前

    }


}
